package com.mitlab.dev.draw.mapper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mitlab.dev.draw.bean.ClassParam;
import com.mitlab.dev.draw.samleClass.Person;

public class NodeData{
	private String key;
	private List<Field> fields;
	private String loc; // x y
	private String category;
	private String text;
	private String className;
	private String methodName;
//	private String resultName;
	private List<MethodResultBean> result;
	private List<MethodResultBean> inputparams;
	private String classkey;
	private String methodkey;
	private int __gohashid;
	public NodeData() {

	}

	public NodeData(Class<?> clazz) {
		this.key = clazz.getName();
		Method[] methods = clazz.getMethods();
		List<Field> fields = new ArrayList<Field>();
		StringBuilder fieldName = new StringBuilder();
		for (Method method : methods) {
			String methodName = method.getName();
			if (methodName.startsWith("set")) {
				Field field = new Field();
				char prefix = methodName.charAt(3);
				if (prefix >= 65 && prefix <= 90) {
					prefix += 32;
				}
				fieldName.append(prefix);
				fieldName.append(methodName.substring(4));
				field.setName(fieldName.toString());
				fieldName.delete(0, fieldName.length());
				fields.add(field);
			}
		}
		this.setFields(fields);
	}

	public static void main(String[] args) throws Exception {
		ObjectMapper om = new ObjectMapper();
		om.writeValue(System.out, new NodeData(Person.class));
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public List<Field> getFields() {
		return fields;
	}

	public void setFields(List<Field> fields) {
		if (fields != null) {
			for (int i = 0; i < fields.size(); i++) {
				fields.get(i).setColor(RGB[i % RGB.length]);
			}
		}
		this.fields = fields;
	}

	public String getLoc() {
		return loc;
	}

	public void setLoc(String loc) {
		this.loc = loc;
	}

	private final static String[] RGB;
	static {
		List<String> colors = new ArrayList<String>();
		BufferedReader lineReader = null;
		try {
			lineReader = new BufferedReader(new InputStreamReader(NodeData.class.getResourceAsStream("rgb.lst")));
			String line = null;
			while ((line = lineReader.readLine()) != null) {
				line = line.trim();
				if (line.equals("")) {
					continue;
				}
				colors.add(line);
			}
			RGB = new String[colors.size()];
			colors.toArray(RGB);
		} catch (Exception e) {
			throw new RuntimeException("init rgb list error", e);
		} finally {
			try {
				if (lineReader != null) {
					lineReader.close();
				}
			} catch (IOException e) {
				throw new RuntimeException("close rgb list error", e);
			}
		}
	}
	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	

	public String getClasskey() {
		return classkey;
	}

	public void setClasskey(String classkey) {
		this.classkey = classkey;
	}

	public String getMethodkey() {
		return methodkey;
	}

	public void setMethodkey(String methodkey) {
		this.methodkey = methodkey;
	}

	public int get__gohashid() {
		return __gohashid;
	}

	public void set__gohashid(int __gohashid) {
		this.__gohashid = __gohashid;
	}

	public List<MethodResultBean> getResult() {
		return result;
	}

	public void setResult(List<MethodResultBean> result) {
		this.result = result;
	}

	public List<MethodResultBean> getInputparams() {
		return inputparams;
	}

	public void setInputparams(List<MethodResultBean> inputparams) {
		this.inputparams = inputparams;
	}

	

}
